
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class FingerTapGuide : MonoBehaviour
{
    public Image fingerImage;  // 手指图像
    public float tapDuration = 0.5f;  // 点击动画持续时间
    public float idleDuration = 0.5f;  // 点击之间的间隔时间
    public Vector3 tapScale = new Vector3(0.8f, 0.8f, 1f);  // 点击时的缩放

    private Vector3 originalScale;

    void Start()
    {
        if (fingerImage == null)
        {
            fingerImage = GetComponent<Image>();
        }

        originalScale = fingerImage.transform.localScale;
        StartCoroutine(AnimateFingerTap());
    }

    IEnumerator AnimateFingerTap()
    {
        while (true)
        {
            // 点击动画
            yield return StartCoroutine(ScaleFinger(originalScale, tapScale, tapDuration / 2));
            yield return StartCoroutine(ScaleFinger(tapScale, originalScale, tapDuration / 2));

            // 等待一段时间再开始下一次点击
            yield return new WaitForSeconds(idleDuration);
        }
    }

    IEnumerator ScaleFinger(Vector3 startScale, Vector3 endScale, float duration)
    {
        float elapsedTime = 0f;
        while (elapsedTime < duration)
        {
            fingerImage.transform.localScale = Vector3.Lerp(startScale, endScale, elapsedTime / duration);
            elapsedTime += Time.deltaTime;
            yield return null;
        }
        fingerImage.transform.localScale = endScale;
    }

    // 可以通过调用这个方法来停止动画
    public void StopAnimation()
    {
        StopAllCoroutines();
        fingerImage.transform.localScale = originalScale;
    }
}